package view;

import dao.CityDao;
import dao.JobInfoDao;
import dao.KeyDao;
import entity.JobInfo;
import lombok.SneakyThrows;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;

import java.awt.event.ActionEvent;
import java.sql.SQLException;
import java.util.List;
import java.util.Vector;

public class ManageJobFrame extends JFrame {
    private int pageNum = 1;
    private int pageSize = 30;
    private int finalPageCount = 0;
    private int count = 0;
    private int pageCount = 0;
    private Vector<String> head = null;
    JobInfoDao jobInfoDao = new JobInfoDao();
    private JTable jTable;
    JComboBox cityJCom = null;
    JComboBox keyCom = null;
   // JTextField cityFiled = null;
   // JTextField jobFiled = null;
    public ManageJobFrame() throws SQLException {
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        this.setBounds(100,40,1200,800);
        this.setTitle("数据展示");

        JPanel jPanelHead = new JPanel();
        Dimension labelDim = new Dimension(80,30);
        Dimension textDim = new Dimension(250,30);
        JLabel cityLabel = new JLabel("城市");
        cityJCom = new JComboBox();
        cityJCom.setPreferredSize(textDim);
        cityJCom.addItem("--请选择--");
        List<String> city = CityDao.checkCity();
        for (String str:city){
            cityJCom.addItem(str);
        }
       // cityFiled = new JTextField();
        JLabel jobLabel = new JLabel("职位");
        keyCom = new JComboBox();
        keyCom.setPreferredSize(textDim);
        keyCom.addItem("--请选择--");
        List<String> key = KeyDao.checkKey();
        for (String str:key){
            keyCom.addItem(str);
        }
       // jobFiled = new JTextField();
       // cityFiled.setPreferredSize(labelDim);
       // cityFiled.setPreferredSize(textDim);
        jobLabel.setPreferredSize(labelDim);
       // jobFiled.setPreferredSize(textDim);

        JButton searchButton = new JButton("搜索");

        jPanelHead.add(cityLabel);
        jPanelHead.add(cityJCom);
        jPanelHead.add(jobLabel);
        jPanelHead.add(keyCom);
        jPanelHead.add(searchButton);
        this.add(jPanelHead,BorderLayout.NORTH);


        jTable = new JTable();
        JScrollPane jScrollPane = new JScrollPane(jTable);
        jTable.getTableHeader().setFont(new Font("黑体",Font.BOLD,24));
        jTable.setFont(new Font("楷体",Font.PLAIN,20));
        jTable.setRowHeight(30);

        head = new Vector<>();
        head.add("工作ID");
        head.add("工作名称");
        head.add("工作城市");
        head.add("公司");
        head.add("工资");
        head.add("工作经验");
        head.add("学历");
        head.add("城市码");
        head.add("职位");
        Vector<Vector<String>> data = new Vector<>();
//        Vector<String> job = new Vector<>();
//        job.add("111");
//        job.add("111");
//        job.add("111");
//        job.add("111");
//        job.add("111");
//        job.add("111");
//        job.add("111");
//        job.add("111");
//        job.add("111");
//        data.add(job);
//        data.add(job);
//        data.add(job);
//        data.add(job);
//        search();
        DefaultTableModel model = new DefaultTableModel(data,head);
        jTable.setModel(model);


        this.add(jScrollPane,BorderLayout.CENTER);


//        count = javaDao.selectCount("");
//        pageCount = 0;
//        if (count%pageSize == 0){
//            pageCount = count/pageSize;
//        }else {
//            pageCount = count/pageSize+1;
//        }






        JPanel jPanelBottom = new JPanel();
        JButton prBtn = new JButton("上一页");
        prBtn.setPreferredSize(labelDim);
   //     finalPageCount = pageCount;
        JTextField jTextField = new JTextField(pageNum-1+"/"+pageCount);
        jTextField.setPreferredSize(labelDim);
        prBtn.addActionListener(new AbstractAction() {
            @SneakyThrows
            @Override
            public void actionPerformed(ActionEvent e) {
                String city = (String) cityJCom.getSelectedItem();
                String cityCode = CityDao.checkCityCode(city);
                String job = (String) keyCom.getSelectedItem();
                if (pageNum == 1){
                    JOptionPane.showMessageDialog(null,"已经是第一页");
                }else {
                    pageNum--;
                    jTextField.setText(pageNum+"/"+finalPageCount);
                    if (city.equals("--请选择--")&& !job.equals("--请选择--")){
                        searchByJob();
                    }else
                    if (!city.equals("--请选择--")&& job.equals("--请选择--")){
                        searchByCity();
                    }else
                    if (city.equals("--请选择--")&& job.equals("--请选择--")){
                        searchAll();
                    }else {
                        search();
                    }

                }
            }
        });


        searchButton.addActionListener(new AbstractAction() {
            @SneakyThrows
            @Override
            public void actionPerformed(ActionEvent e) {
                pageNum = 1;
                String city = (String) cityJCom.getSelectedItem();
                String cityCode = CityDao.checkCityCode(city);
                String job = (String) keyCom.getSelectedItem();
                if (city.equals("--请选择--")& !job.equals("--请选择--")){
                    count = jobInfoDao.selectCountByJob(job);

                    if (count%pageSize == 0){
                        pageCount = count/pageSize;
                    }else {
                        pageCount = count/pageSize+1;
                    }
                    finalPageCount = pageCount;

                    searchByJob();

                    jTextField.setText(pageNum+"/"+finalPageCount);

                }else
                if (!city.equals("--请选择--") & job.equals("--请选择--")){
                    count = jobInfoDao.selectCountByCity(cityCode);

                    if (count%pageSize == 0){
                        pageCount = count/pageSize;
                    }else {
                        pageCount = count/pageSize+1;
                    }
                    finalPageCount = pageCount;
                    searchByCity();
                    jTextField.setText(pageNum+"/"+finalPageCount);
                }else
                if (city.equals("--请选择--") & job.equals("--请选择--")){
                    count = jobInfoDao.selectCountAll();

                    if (count%pageSize == 0){
                        pageCount = count/pageSize;
                    }else {
                        pageCount = count/pageSize+1;
                    }
                    finalPageCount = pageCount;
                    searchAll();
                    jTextField.setText(pageNum+"/"+finalPageCount);
                }else {
                    count = jobInfoDao.selectCount(cityCode,job);

                    if (count%pageSize == 0){
                        pageCount = count/pageSize;
                    }else {
                        pageCount = count/pageSize+1;
                    }
                    finalPageCount = pageCount;
                    search();
                    jTextField.setText(pageNum+"/"+finalPageCount);
                }


            }

        });


        JButton nextBtn = new JButton("下一页");
        nextBtn.setPreferredSize(labelDim);
        nextBtn.addActionListener(new AbstractAction() {
            @SneakyThrows
            @Override
            public void actionPerformed(ActionEvent e) {
                String city = (String) cityJCom.getSelectedItem();
                String cityCode = CityDao.checkCityCode(city);
                String job = (String) keyCom.getSelectedItem();
                if (pageNum == finalPageCount || pageCount == 0){
                    JOptionPane.showMessageDialog(null,"已经是最后一页");
                }else {

                    pageNum++;
                    jTextField.setText(pageNum+"/"+finalPageCount);
                    if (city.equals("--请选择--")&& !job.equals("--请选择--")){
                        searchByJob();
                    }else
                    if (!city.equals("--请选择--")&& job.equals("--请选择--")){
                        searchByCity();
                    }else
                    if (city.equals("--请选择--")&& job.equals("--请选择--")){
                        searchAll();
                    }else {
                        search();
                    }

                }
            }
        });

        JButton firstBtn = new JButton("首页");
        firstBtn.setPreferredSize(labelDim);

        firstBtn.addActionListener(new AbstractAction() {
            @SneakyThrows
            @Override
            public void actionPerformed(ActionEvent e) {
                String city = (String) cityJCom.getSelectedItem();
                String cityCode = CityDao.checkCityCode(city);
                String job = (String) keyCom.getSelectedItem();
                if (pageNum-1 == 0){
                }else {
                    pageNum = 1;
                    jTextField.setText(pageNum+"/"+finalPageCount);
                    if (city.equals("--请选择--")&& !job.equals("--请选择--")){
                        searchByJob();
                    }else
                    if (!city.equals("--请选择--")&& job.equals("--请选择--")){
                        searchByCity();
                    }else
                    if (city.equals("--请选择--")&& job.equals("--请选择--")){
                        searchAll();
                    }else {
                        search();
                    }
                }
            }
        });




        JButton endBtn = new JButton("尾页");
        endBtn.setPreferredSize(labelDim);

        endBtn.addActionListener(new AbstractAction() {
            @SneakyThrows
            @Override
            public void actionPerformed(ActionEvent e) {
                String city = (String) cityJCom.getSelectedItem();
                String cityCode = CityDao.checkCityCode(city);
                String job = (String) keyCom.getSelectedItem();
                pageNum = finalPageCount;
                jTextField.setText(pageNum+"/"+finalPageCount);
                if (city.equals("--请选择--")&& !job.equals("--请选择--")){
                    searchByJob();
                }else
                if (!city.equals("--请选择--")&& job.equals("--请选择--")){
                    searchByCity();
                }else
                if (city.equals("--请选择--")&& job.equals("--请选择--")){
                    searchAll();
                }else {
                    search();
                }
            }
        });
        jPanelBottom.add(firstBtn);
        jPanelBottom.add(prBtn);
        jPanelBottom.add(jTextField);
        jPanelBottom.add(nextBtn);
        jPanelBottom.add(endBtn);
        this.add(jPanelBottom,BorderLayout.SOUTH);



    }

    private void search() throws SQLException {
        String city = (String) cityJCom.getSelectedItem();
        String cityCode = CityDao.checkCityCode(city);
        String job = (String) keyCom.getSelectedItem();

        List<JobInfo> javaList = jobInfoDao.selectByPage(pageNum,pageSize,job,cityCode);
        Vector<Vector<String>> alljob = new Vector<>();
        for (JobInfo jobInfo:javaList){
            Vector<String> thisjob = new Vector<>();
            thisjob.add(jobInfo.getJobId());
            thisjob.add(jobInfo.getJonName());
            thisjob.add(jobInfo.getJobCity());
            thisjob.add(jobInfo.getCompany());
            thisjob.add(jobInfo.getSalary());
            thisjob.add(jobInfo.getExperience());
            thisjob.add(jobInfo.getEd());
            thisjob.add(jobInfo.getCityCode());
            thisjob.add(jobInfo.getJob());
            alljob.add(thisjob);
        }
        DefaultTableModel model = new DefaultTableModel(alljob,head);
        jTable.setModel(model);

    }
    private void searchByJob() throws SQLException {

        String job = (String) keyCom.getSelectedItem();

        List<JobInfo> javaList = jobInfoDao.selectByJob(pageNum,pageSize,job);
        Vector<Vector<String>> alljob = new Vector<>();
        for (JobInfo jobInfo:javaList){
            Vector<String> thisjob = new Vector<>();
            thisjob.add(jobInfo.getJobId());
            thisjob.add(jobInfo.getJonName());
            thisjob.add(jobInfo.getJobCity());
            thisjob.add(jobInfo.getCompany());
            thisjob.add(jobInfo.getSalary());
            thisjob.add(jobInfo.getExperience());
            thisjob.add(jobInfo.getEd());
            thisjob.add(jobInfo.getCityCode());
            thisjob.add(jobInfo.getJob());
            alljob.add(thisjob);
        }
        DefaultTableModel model = new DefaultTableModel(alljob,head);
        jTable.setModel(model);

    }
    private void searchByCity() throws SQLException {
        String city = (String) cityJCom.getSelectedItem();
        String cityCode = CityDao.checkCityCode(city);


        List<JobInfo> javaList = jobInfoDao.selectByCity(pageNum,pageSize,cityCode);
        Vector<Vector<String>> alljob = new Vector<>();
        for (JobInfo jobInfo:javaList){
            Vector<String> thisjob = new Vector<>();
            thisjob.add(jobInfo.getJobId());
            thisjob.add(jobInfo.getJonName());
            thisjob.add(jobInfo.getJobCity());
            thisjob.add(jobInfo.getCompany());
            thisjob.add(jobInfo.getSalary());
            thisjob.add(jobInfo.getExperience());
            thisjob.add(jobInfo.getEd());
            thisjob.add(jobInfo.getCityCode());
            thisjob.add(jobInfo.getJob());
            alljob.add(thisjob);
        }
        DefaultTableModel model = new DefaultTableModel(alljob,head);
        jTable.setModel(model);

    }
    private void searchAll() throws SQLException {
        String city = (String) cityJCom.getSelectedItem();
        String cityCode = CityDao.checkCityCode(city);


        List<JobInfo> javaList = jobInfoDao.selectAll(pageNum,pageSize);
        Vector<Vector<String>> alljob = new Vector<>();
        for (JobInfo jobInfo:javaList){
            Vector<String> thisjob = new Vector<>();
            thisjob.add(jobInfo.getJobId());
            thisjob.add(jobInfo.getJonName());
            thisjob.add(jobInfo.getJobCity());
            thisjob.add(jobInfo.getCompany());
            thisjob.add(jobInfo.getSalary());
            thisjob.add(jobInfo.getExperience());
            thisjob.add(jobInfo.getEd());
            thisjob.add(jobInfo.getCityCode());
            thisjob.add(jobInfo.getJob());
            alljob.add(thisjob);
        }
        DefaultTableModel model = new DefaultTableModel(alljob,head);
        jTable.setModel(model);

    }
}
